/* 这是验证处理函数模块 */

const db = require('../../db')


exports.isVip = (req, res) => {
  const ID = req.params.ID;
  const sql = `select is_VIP from module3_users where user_id = ?`
  db.query(sql, ID, (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此用户不存在')
      res.send({
      status: 0,
      message: '查询成功',
      data: results
      })
  })
}

exports.isBusinessmen = (req, res) => {
  const ID = req.params.ID;
  const sql = `select is_businessmen from module3_users where user_id = ?`
  db.query(sql, ID, (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此用户不存在')
      res.send({
      status: 0,
      message: '查询成功',
      data: results
      })
  })
}

exports.isBlacklist = (req, res) => {
  const ID = req.params.ID;
  const sql = `select is_blacklist from module3_users where user_id = ?`
  db.query(sql, ID, (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此用户不存在')
      res.send({
      status: 0,
      message: '查询成功',
      data: results
      })
  })
}

exports.isInspector = (req, res) => {
  const ID = req.params.ID;
  const sql = `select is_Inspector from module3_users where user_id = ?`
  db.query(sql, ID, (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此用户不存在')
      res.send({
      status: 0,
      message: '查询成功',
      data: results
      })
  })
}
exports.isAdministrator = (req, res) => {
  const ID = req.params.ID;
  const sql = `select is_administrator from module3_users where user_id = ?`
  db.query(sql, ID, (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此用户不存在')
      res.send({
      status: 0,
      message: '查询成功',
      data: results
      })
  })
}
exports.bankPay = (req, res) => {
    const No = req.params.No;
    const Password = req.params.Password;
    const Balance = req.params.Balance;
    const sql = `select * from Bank where Card_No = ?`
    db.query(sql, [No], (err, results) => {
        if(err) return res.cc(err)
        if(results.length !== 1) return res.cc('此卡不存在')
        if(results[0].Password!=Password) return  (
            res.send({
            status: 2,
            message: '密码错误',
            }))
        if(results[0].Balance-parseInt(Balance)<0) return  (
            res.send({
            status: 3,
            message: '余额不足',
            }))
        const sql = `update bank set Balance = Balance - ? where Card_No = ?`
        db.query(sql, [Balance,No], (err, results) => {
            if(err) return res.cc(err)
            const sql = `select Balance from Bank where Card_No = ?`
            db.query(sql, [No], (err, results) => {
                if(err) return res.cc(err)
                res.send({
                status: 0,
                message: '余额更新成功',
                data: results
                })
            })
        })
    })
}
exports.bankRecharge = (req, res) => {
  const No = req.params.No;
  const Password = req.params.Password;
  const Balance = req.params.Balance;
  const sql = `select * from Bank where Card_No = ?`
  db.query(sql, [No,Password], (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此卡不存在')
      const sql = `update bank set Balance = Balance + ? where Card_No = ?`
      db.query(sql, [Balance,No], (err, results) => {
          if(err) return res.cc(err)
          const sql = `select Balance from Bank where Card_No = ?`
          db.query(sql, [No], (err, results) => {
              if(err) return res.cc(err)
              res.send({
              status: 0,
              message: '余额更新成功',
              data: results
              })
          })
      })
  })
}
exports.realNameVerify = (req, res) => {
    const ID = req.params.ID;
    const IC = req.params.IC;
    const RealName = req.params.RealName;
    const sql = `select * from ICDatabase where IC = ?`
    db.query(sql,IC, (err, results) => {
        console.log(results);
        if(err) return res.cc(err)
        if(results.length !== 1) return res.cc('不存在此人')
        console.log(results[0].RealName,RealName);
        if(results[0].RealName!=RealName) return  (
            res.send({
            status: 2,
            message: '验证失败',
            }))
        res.send({
        status: 0,
        message: '实名认证成功',
        })
    })
}

exports.getBankCard = (req, res) => {
  const Card_No = req.params.No;
  const sql = `select Balance from Bank where Card_No = ?`
  db.query(sql, Card_No, (err, results) => {
      if(err) return res.cc(err)
      if(results.length !== 1) return res.cc('此卡不存在')
      res.send({
      status: 0,
      message: '查找成功',
      data: results
      })
  })
}
exports.GetPrePaidCard = (req, res) => {
    const Card_No = req.params.No;
    const sql = `select Balance from PrepaidCard where Card_No = ?`
    db.query(sql, Card_No, (err, results) => {
        if(err) return res.cc(err)
        if(results.length !== 1) return res.cc('此卡不存在')
        res.send({
        status: 0,
        message: '查找成功',
        data: results
        })
    })
}
exports.DropBankCard = (req, res) => {
    const Card_No = req.params.No;
    const sql = `delete from Bank where Card_No = ?`
    db.query(sql, Card_No, (err, results) => {
        if(err) return res.cc(err)
        res.cc('删除成功！', 0)
    })
}
exports.DropICDatabase = (req, res) => {
    const IC = req.params.IC;
    const sql = `delete from ICDataBase where IC = ?`
    db.query(sql, IC, (err, results) => {
        if(err) return res.cc(err)
        res.cc('删除成功！', 0)
    })
}
exports.DropPrePaidCard = (req, res) => {
    const Card_No = req.params.No;
    const sql = `delete from PrepaidCard where Card_No = ?`
    db.query(sql, Card_No, (err, results) => {
        if(err) return res.cc(err)
        res.cc('删除成功！', 0)
    })
}


exports.insertBankCard = (req, res) => {
    Card_No=req.params.No;
    Password=req.params.Password;
    Balance=req.params.Balance;
    const sql = 'insert into Bank(Card_No,Password,Balance) values (?,?,?)'
    db.query(sql,[Card_No,Password,Balance], (err, results) => {
      if(err) return res.cc(err)
      res.send({
        status: 0,
        message: '数据插入成功',
      })
    })
}
exports.insertIC = (req, res) => {
    IC=req.params.IC;
    RealName=req.params.RealName;
    const sql = 'insert into ICDataBase(IC,RealName) values (?,?)'
    db.query(sql, [IC,RealName],(err, results) => {
      if(err) return res.cc(err)
      res.send({
        status: 0,
        message: '数据插入成功',
      })
    })
}
exports.insertPrepaid = (req, res) => {
    console.log(req.params)
    Card_No=req.params.CardNo;
    Balance=req.params.Balance;
    const sql = 'insert into PrepaidCard(Card_No,Balance) values (?,?)'
    db.query(sql, [Card_No,Balance],(err, results) => {
      if(err) return res.cc(err)
      res.send({
        status: 0,
        message: '数据插入成功',
      })
    })
}
exports.getAllBankCard = (req, res) => {
    const sql = 'select * from Bank'
    db.query(sql, (err, results) => {
      if(err) return res.cc(err)
      res.send({
        status: 0,
        message: '数据返回成功',
        data: results,
      })
    })
}
exports.getAllIC = (req, res) => {
    const sql = 'select * from ICDatabase'
    db.query(sql, (err, results) => {
      if(err) return res.cc(err)
      res.send({
        status: 0,
        message: '数据返回成功',
        data: results,
      })
    })
}
exports.getAllPrePaid = (req, res) => {
    const sql = 'select * from PrepaidCard'
    db.query(sql, (err, results) => {
      if(err) return res.cc(err)
      res.send({
        status: 0,
        message: '数据返回成功',
        data: results,
      })
    })
}